package cn.lysj.framework.generator;

import cn.hutool.core.date.DateUtil;
import cn.lysj.framework.common.utils.web.ParamUtil;
import cn.lysj.framework.generator.bean.EntityOfEntity;
import lombok.extern.slf4j.Slf4j;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.FileResourceLoader;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 代码生成器
 * @author Exrickx
 */
@Slf4j
public class Generator {

    private static final String module = "verify";//模块名 必填项 修改即可

    /**
     * 实体类名
     * 建议仅需修改
     */
    private static final String className = "OffAcctOpenidVerify";

    /**
     * 类说明描述
     * 建议仅需修改
     */
    private static final String description = "公众号openid校验";

    /**
     * 作者名
     * 建议仅需修改
     */
    private static final String author = "drj";

    /**
     * 主键类型
     */
    private static final String primaryKeyType = "String";

    /**
     * 实体类对应包
     */
    private static final String entityPackage = "cn.lysj.framework."+module+".domain";

    /**
     * dao对应包
     */
    private static final String daoPackage = "cn.lysj.framework."+module+".repository";

    /**
     * service对应包
     * 不启用 暂不需要service+serviceImpl结构
     */
    private static final String servicePackage = "";

    /**
     * serviceImpl对应包
     */
    private static final String serviceImplPackage = "cn.lysj.framework."+module+".service";

    /**
     * controller对应包
     */
    private static final String controllerPackage = "cn.lysj.framework."+module+".controller";

    /**
     * 运行该主函数即可生成代码
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {

        //模板路径
        String root = System.getProperty("user.dir")+"/src/main/java/cn/lysj/framework/generator/template";
        FileResourceLoader resourceLoader = new FileResourceLoader(root,"utf-8");
        Configuration cfg = Configuration.defaultConfiguration();
        GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);

        //生成代码
        generateCode(gt);

        //根据类名删除生成的代码
//        deleteCode(className);
    }

    /**
     * 生成代码
     * @param gt
     * @throws IOException
     */
    private static void generateCode(GroupTemplate gt) throws IOException{

        Template entityTemplate = gt.getTemplate("entity.btl");
        Template daoTemplate = gt.getTemplate("dao.btl");
//        Template serviceTemplate = gt.getTemplate("service.btl");
        Template serviceImplTemplate = gt.getTemplate("serviceImpl.btl");
        Template controllerTemplate = gt.getTemplate("controller.btl");

        EntityOfEntity entity = new EntityOfEntity();
        entity.setEntityPackage(entityPackage);
        entity.setDaoPackage(daoPackage);
        entity.setServicePackage(servicePackage);
        entity.setServiceImplPackage(serviceImplPackage);
        entity.setControllerPackage(controllerPackage);
        entity.setAuthor(author);
        entity.setClassName(className);
        entity.setUnderlineName(ParamUtil.camel2Underline(className));
//        entity.setTableName("t_"+camel2Underline(className));
        entity.setClassNameLowerCase(ParamUtil.first2LowerCase(className));
        entity.setDescription(description);
        entity.setPrimaryKeyType(primaryKeyType);
        entity.setCreateDate(DateUtil.now());

        OutputStream out = null;

        //生成实体类代码
        entityTemplate.binding("entity",entity);
        String entityResult = entityTemplate.render();
        log.info(entityResult);
        //创建文件
        String entityFileUrl = System.getProperty("user.dir")+"/src/main/java/cn/lysj/framework/"+module+"/domain/"+className+".java";
        File entityFile = new File(entityFileUrl);
        String entityDirUrl = System.getProperty("user.dir")+"/src/main/java/cn/lysj/framework/"+module+"/domain/";
        File entityDir = new File(entityDirUrl);
        if(!entityDir .isDirectory()){
            entityDir.mkdirs();
        }
        if(!entityFile.exists()){
            //实体类若存在则不重新生成
            entityFile.createNewFile();
            out = new FileOutputStream(entityFile);
            entityTemplate.renderTo(out);
        }

        //生成dao代码
        daoTemplate.binding("entity",entity);
        String daoResult = daoTemplate.render();
        log.info(daoResult);
        //创建文件
        String daoFileUrl = System.getProperty("user.dir")+"/src/main/java/cn/lysj/framework/"+module+"/repository/"+className+"Repository.java";
        File daoFile = new File(daoFileUrl);
        String daoDirUrl = System.getProperty("user.dir")+"/src/main/java/cn/lysj/framework/"+module+"/repository/";
        File daoDir = new File(daoDirUrl);
        if(!daoDir .isDirectory()){
            daoDir.mkdirs();
        }
        if(!daoFile.exists()){
            daoFile.createNewFile();
            out = new FileOutputStream(daoFile);
            daoTemplate.renderTo(out);
        }
//        //生成service代码
//        serviceTemplate.binding("entity",entity);
//        String serviceResult = serviceTemplate.render();
//        log.info(serviceResult);
//        //创建文件
//        String serviceFileUrl = System.getProperty("user.dir")+"/src/main/java/cn/lysj/framework/"+module+"/service/"+className+"Service.java";
//        File serviceFile = new File(serviceFileUrl);
//        serviceFile.createNewFile();
//        out = new FileOutputStream(serviceFile);
//        serviceTemplate.renderTo(out);

        //生成serviceImpl代码
        serviceImplTemplate.binding("entity",entity);
        String serviceImplResult = serviceImplTemplate.render();
        log.info(serviceImplResult);
        //创建文件
        String serviceImplFileUrl = System.getProperty("user.dir")+"/src/main/java/cn/lysj/framework/"+module+"/service/"+className+"Service.java";
        File serviceImplFile = new File(serviceImplFileUrl);
        String serviceDirUrl = System.getProperty("user.dir")+"/src/main/java/cn/lysj/framework/"+module+"/service/";
        File serviceDir = new File(serviceDirUrl);
        if(!serviceDir .isDirectory()){
            serviceDir.mkdirs();
        }
        if(!serviceImplFile.exists()){
            serviceImplFile.createNewFile();
            out = new FileOutputStream(serviceImplFile);
            serviceImplTemplate.renderTo(out);
        }


        //生成controller代码
        controllerTemplate.binding("entity",entity);
        String controllerResult = controllerTemplate.render();
        log.info(controllerResult);
        //创建文件
        String controllerFileUrl = System.getProperty("user.dir")+"/src/main/java/cn/lysj/framework/"+module+"/controller/"+className+"Controller.java";
        File controllerFile = new File(controllerFileUrl);
        String controllerDirUrl = System.getProperty("user.dir")+"/src/main/java/cn/lysj/framework/"+module+"/controller/";
        File controllerDir = new File(controllerDirUrl);
        if(!controllerDir .isDirectory()){
            controllerDir.mkdirs();
        }
        if(!controllerFile.exists()){
            controllerFile.createNewFile();
            out = new FileOutputStream(controllerFile);
            controllerTemplate.renderTo(out);
        }
        out.close();
        log.info("=====生成代码成功=====");
    }

    /**
     * 删除指定类代码
     * @param className
     * @throws IOException
     */
    private static void deleteCode(String className) throws IOException{

        String entityFileUrl = System.getProperty("user.dir")+"/src/main/java/cn/lysj/framework/"+module+"/domain/"+className+".java";
        File entityFile = new File(entityFileUrl);
        if(entityFile.exists()){
            entityFile.delete();
        }
        String daoFileUrl = System.getProperty("user.dir")+"/src/main/java/cn/lysj/framework/"+module+"/repository/"+className+"Repository.java";
        File daoFile = new File(daoFileUrl);
        if(daoFile.exists()){
            daoFile.delete();
        }

//        String serviceFileUrl = System.getProperty("user.dir")+"/src/main/java/cn/lysj/framework/service/"+className+"Service.java";
//        File serviceFile = new File(serviceFileUrl);
//        if(serviceFile.exists()){
//            serviceFile.delete();
//        }

        String serviceImplFileUrl = System.getProperty("user.dir")+"/src/main/java/cn/lysj/framework/"+module+"/service/"+className+"Service.java";
        File serviceImplFile = new File(serviceImplFileUrl);
        if(serviceImplFile.exists()){
            serviceImplFile.delete();
        }

        String controllerFileUrl = System.getProperty("user.dir")+"/src/main/java/cn/lysj/framework/"+module+"/controller/"+className+"Controller.java";
        File controllerFile = new File(controllerFileUrl);
        if(controllerFile.exists()){
            controllerFile.delete();
        }

        log.info("=====删除代码成功=====");
    }

}
